#include "utils.hpp"

#include <prometheus/histogram.h>
#include <stdexcept>

namespace utils {
    std::int64_t get_timestamp_ns() {
        // Get current time with nanosecond precision. Use system_clock instead of steady_clock to measure duration across different servers.
        const auto now = std::chrono::system_clock::now();

        // Convert to nanoseconds since epoch.
        return std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
    }

    std::string get_env(const char *name) {
        const char *value = std::getenv(name);
        if (!value) {
            throw std::runtime_error(std::string("Environment variable ") + name + " not found");
        }
        return value;
    }

    prometheus::Histogram::BucketBoundaries get_hist_buckets() {
        // clang-format off
    static const std::vector<double> buckets = {
        0, 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100,
        150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000,
        1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500,
        9000, 9500, 10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000, 20000,
        25000, 30000, 35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000, 75000, 80000,
        85000, 90000, 95000, 100000,
        101000, 102000, 103000, 104000, 105000, 106000, 107000, 108000, 109000, 110000,
        111000, 112000, 113000, 114000, 115000, 116000, 117000, 118000, 119000, 120000,
        121000, 122000, 123000, 124000, 125000, 126000, 127000, 128000, 129000, 130000,
        131000, 132000, 133000, 134000, 135000, 136000, 137000, 138000, 139000, 140000,
        141000, 142000, 143000, 144000, 145000, 146000, 147000, 148000, 149000, 150000,
        151000, 152000, 153000, 154000, 155000, 156000, 157000, 158000, 159000, 160000,
        161000, 162000, 163000, 164000, 165000, 166000, 167000, 168000, 169000, 170000,
        171000, 172000, 173000, 174000, 175000, 176000, 177000, 178000, 179000, 180000,
        181000, 182000, 183000, 184000, 185000, 186000, 187000, 188000, 189000, 190000,
        191000, 192000, 193000, 194000, 195000, 196000, 197000, 198000, 199000, 200000,
        210000, 220000, 230000, 240000, 250000, 260000, 270000, 280000, 290000, 300000,
        310000, 320000, 330000, 340000, 350000, 360000, 370000, 380000, 390000, 400000,
        410000, 420000, 430000, 440000, 450000, 460000, 470000, 480000, 490000, 500000,
        510000, 520000, 530000, 540000, 550000, 560000, 570000, 580000, 590000, 600000,
        610000, 620000, 630000, 640000, 650000, 660000, 670000, 680000, 690000, 700000,
        710000, 720000, 730000, 740000, 750000, 760000, 770000, 780000, 790000, 800000,
        810000, 820000, 830000, 840000, 850000, 860000, 870000, 880000, 890000, 900000,
        910000, 920000, 930000, 940000, 950000, 960000, 970000, 980000, 990000, 1000000,
        1500000, 2000000, 2500000, 3000000, 3500000, 4000000, 4500000, 5000000,
        5500000, 6000000, 6500000, 7000000, 7500000, 8000000, 8500000, 9000000,
        9500000, 10000000, 15000000, 20000000, 25000000, 30000000, 35000000,
        40000000, 45000000, 50000000, 55000000, 60000000, 65000000, 70000000,
        75000000, 80000000, 85000000, 90000000, 95000000, 100000000,
        150000000, 200000000, 250000000, 300000000, 350000000, 400000000,
        450000000, 500000000, 550000000, 600000000, 650000000, 700000000,
        750000000, 800000000, 850000000, 900000000, 950000000,
        1000000000, 1500000000, 2000000000, 2500000000, 3000000000,
        3500000000, 4000000000, 4500000000, 5000000000
    };
        // clang-format on

        return prometheus::Histogram::BucketBoundaries{buckets};
    }
} // namespace utils
